package com.cx.mall.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.cx.mall.common.entity.user.entity.UserReceiveAddress;
import com.cx.mall.common.entity.user.model.UserReceiveAddressDTO;
import com.cx.mall.user.mapper.UserReceiveAddressMapper;
import com.cx.mall.user.service.IUserReceiveAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户收货地址表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2025-08-07
 */
@Slf4j
@Service
public class UserReceiveAddressServiceImpl extends ServiceImpl<UserReceiveAddressMapper, UserReceiveAddress> implements IUserReceiveAddressService {

    @Override
    public List<UserReceiveAddressDTO> getListByUserId(Long userId) {
        QueryChainWrapper<UserReceiveAddress> chainWrapper = new QueryChainWrapper<>(baseMapper);
        List<UserReceiveAddress> userReceiveAddressList = chainWrapper.eq("user_id", userId).list();
        //TODO 转换为DTO
       List<UserReceiveAddressDTO> userReceiveAddressDTOList = userReceiveAddressList.stream().map(address ->
                JSON.parseObject(JSON.toJSONString(address), UserReceiveAddressDTO.class))
                .collect(Collectors.toList());
       log.info("用户地址列表查询成功,用户地址列表:{}",userReceiveAddressDTOList);
       return userReceiveAddressDTOList;

    }
}
